package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.dao.AlbumDao;
import cn.sumpu.app.guoguojie.domain.Album;
import cn.sumpu.app.guoguojie.domain.AlbumDoing;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class AlbumDaoImpl extends SqlSessionDaoSupport implements AlbumDao {

    @Override
    public void insertAlbum(Album album) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.AlbumMapper.insertAlbum", album);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertAlbum failure.", e);
            }
            throw new AppBusinessException("insertAlbum failure.", e);
        }
    }

    @Override
    public void updateAlbum(Album album) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.AlbumMapper.updateAlbum", album);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateAlbum failure.", e);
            }
            throw new AppBusinessException("updateAlbum failure.", e);
        }
    }

    @Override
    public void deleteAlbum(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.AlbumMapper.deleteAlbum", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteAlbum failure.", e);
            }
            throw new AppBusinessException("deleteAlbum failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Album> getAlbum(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Album>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbum", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbum failure.", e);
            }
            throw new AppBusinessException("getAlbum failure.", e);
        }
    }

    @Override
    public int getAlbumCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbumCount failure.", e);
            }
            throw new AppBusinessException("getAlbumCount failure.", e);
        }
    }

    @Override
    public void insertAlbumDoing(AlbumDoing albumDoing) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.AlbumMapper.insertAlbumDoing", albumDoing);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertAlbumDoing failure.", e);
            }
            throw new AppBusinessException("insertAlbumDoing failure.", e);
        }
    }

    @Override
    public void updateAlbumDoing(AlbumDoing albumDoing) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.AlbumMapper.updateAlbumDoing", albumDoing);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateAlbumDoing failure.", e);
            }
            throw new AppBusinessException("updateAlbumDoing failure.", e);
        }
    }

    @Override
    public void deleteAlbumDoing(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.AlbumMapper.deleteAlbumDoing", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteAlbumDoing failure.", e);
            }
            throw new AppBusinessException("deleteAlbumDoing failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<AlbumDoing> getAlbumDoing(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<AlbumDoing>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumDoing", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbumDoing failure.", e);
            }
            throw new AppBusinessException("getAlbumDoing failure.", e);
        }
    }

    @Override
    public int getAlbumDoingCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumDoingCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbumDoingCount failure.", e);
            }
            throw new AppBusinessException("getAlbumDoingCount failure.", e);
        }
    }

    @Override
    public int getAlbumMinSort(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumMinSort", map);
            return (Integer) (obj == null ? 1 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMinSort failure.", e);
            }
            throw new AppBusinessException("getMinSort failure.", e);
        }
    }

    @Override
    public int getAlbumMaxSort(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumMaxSort", map);
            return (Integer) (obj == null ? 1 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMaxSort failure.", e);
            }
            throw new AppBusinessException("getMaxSort failure.", e);
        }
    }

    @Override
    public int getAlbumDoingMaxSort(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumDoingMaxSort", map);
            return (Integer) (obj == null ? 1 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbumDoingMaxSort failure.", e);
            }
            throw new AppBusinessException("getAlbumDoingMaxSort failure.", e);
        }
    }

    @Override
    public int getAlbumDoingMinSort(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumDoingMinSort", map);
            return (Integer) (obj == null ? 1 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbumDoingMinSort failure.", e);
            }
            throw new AppBusinessException("getAlbumDoingMinSort failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Album> getAlbumRelationDoing(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Album>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumRelationAlbumDoing", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAlbumRelationDoing failure.", e);
            }
            throw new AppBusinessException("getAlbumRelationDoing failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Doing> getDoing(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Doing>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.AlbumMapper.getDoing", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDoing failure.", e);
            }
            throw new AppBusinessException("getDoing failure.", e);
        }
    }
}
